#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import lineage_stage4
import multiprocessing
from fm_util import Util
from fm_param import FmConst


class Stage4LineageOs:

    def __init__(self, infoPrinter, machine, version):
        self._ver = version

    def downloadFiles(self, bTitle):
        if bTitle:
            self._p.printInfo("- Downloading %s %s files..." % (Util.getOsName("lineage"), self._ver))

    def build(self, workDir):
        # step
        self._p.printInfo("- Initializing...")
        wdir = lineage_stage4.WorkDir(workDir, reset=True)
        builder = None
        if True:
            s = lineage_stage4.Settings()
            s.program_name = FmConst.programName
            s.log_dir = None
            s.verbose_level = 2
            s.host_cpu_core_count = multiprocessing.cpu_count()
            s.host_memory_size = Util.getPhysicalMemorySize() * 1024 * 1024 * 1024
            s.host_cooling_level = 10 if Util.hwcfgHasFan(self._machine) is not None else 1

            ts = lineage_stage4.TargetSettings()

            builder = lineage_stage4.Builder(s, ts, wdir)
            builder.action_initialize()

        # step
        self._p.printInfo("- Fetch...")
        builder.action_fetch_source()

        # step
        self._p.printInfo("- Build...")
        builder.action_build()

        # step
        self._p.printInfo("- Install core applications...")
        builder.action_install_core_applications()

        self._p.printInfo("- Cleaning up...")
        builder.action_cleanup()

        # finish
        builder.finish()

        return wdir
